<template>
  <a-modal
    title="审核"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['outRecordId']"/>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="状态"
      >
        <a-radio-group
          @change="radioChange"
          v-decorator="['outStatus', {initialValue:'2',rules: [{ required: true, message: '请选择审核状态' }]}]"
        >
          <a-radio
            :value="item.value"
            v-for="(item, key) in radioList"
            :key="key"
          >
            {{ item.label }}
          </a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="审核原因"
      >
        <a-textarea
          placeholder="请输入转诊原因"
          :rows="5"
          v-decorator="['outApprovalNote', {initialValue:'符合转诊要求，审核通过', rules: [{ required: true, max: 100, message: '请输入转诊原因,且字数不能超过100' }]}]"/>
      </a-form-item>

    </a-form>
  </a-modal>
</template>
<script>
import { refOutApproval } from '@/api/referral'
import pick from 'lodash.pick'
export default {
  name: 'RolloutCheckModal',
  props: {
  },
  components: {
  },
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this),
      radioList: {} // approvalStatus.filter((item) => { return item.value === '2' || item.value === '3' })
    }
  },
  beforeCreate () {
  },
  created () {
    this.getFormDict('refapprovalStatus').then(list => {
      this.radioList = list.filter((item) => { return item.value === '2' || item.value === '3' })
    })
  },
  methods: {
    // add () {
    //   this.form.resetFields()
    //   this.edit({ dictId: 0 })
    // },
    edit (record) {
      this.mdl = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'outRecordId'))
      })
    },
    radioChange (e) {
      if (e.target.value === '2') {
        this.form.setFieldsValue({ 'outApprovalNote': '符合转诊要求，审核通过' })
      } else {
        this.form.setFieldsValue({ 'outApprovalNote': '不符合转诊要求，审核不通过' })
      }
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('审核提交: ', values)
          this.confirmLoading = true
          refOutApproval(values).then(res => {
            this.confirmLoading = false
            if (res.code === 200) {
              this.$message.success('审核成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.message)
            }
          }).catch((err) => {
            this.confirmLoading = false
            this.$message.error(err.message)
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
